package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.util.PoiUtil;
import cn.stylefeng.guns.modular.gridsystem.entity.BizDeviceStatusAssess;
import cn.stylefeng.guns.modular.gridsystem.entity.BizDeviceStatusAssessInfo;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizDeviceStatusAssessMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizDeviceStatusAssessParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.DeviceStatusAssessInfoResult;
import cn.stylefeng.guns.modular.gridsystem.service.BizDeviceStatusAssessInfoService;
import cn.stylefeng.guns.modular.gridsystem.service.BizDeviceStatusAssessService;
import cn.stylefeng.guns.modular.gridsystem.service.BizExceptionService;
import cn.stylefeng.guns.sys.modular.user.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 通道类型 服务实现类
 *
 * @author ssy
 * @date 2022/02/16 10:59
 */
@Service
public class BizDeviceStatusAssessServiceImpl extends ServiceImpl<BizDeviceStatusAssessMapper, BizDeviceStatusAssess> implements BizDeviceStatusAssessService {

    @Resource
    private BizDeviceStatusAssessInfoService bizDeviceStatusAssessInfoService;

    @Override
    public PageResult<Map<String, Object>> page(BizDeviceStatusAssessParam bizDeviceStatusAssessParam) {

        // 构造条件
        LambdaQueryWrapper<BizDeviceStatusAssess> queryWrapper = new LambdaQueryWrapper<>();
        if(bizDeviceStatusAssessParam!=null&& ObjectUtil.isNotEmpty(bizDeviceStatusAssessParam.getDeviceId())){
            queryWrapper.eq(BizDeviceStatusAssess::getDeviceId,bizDeviceStatusAssessParam.getDeviceId());
        }
        Integer[] list = new Integer[1];
        if(bizDeviceStatusAssessParam!=null&& ObjectUtil.isNotEmpty(bizDeviceStatusAssessParam.getDeviceIds())){
            String gatewayIds = bizDeviceStatusAssessParam.getDeviceIds();
            if(StringUtils.isNotBlank(gatewayIds)){
                String[] split = gatewayIds.split(",");
                list = Convert.toIntArray(split);
            }
        }else {
            list[0] = 0;
        }
        queryWrapper.in(BizDeviceStatusAssess::getDeviceId,list);
        // 查询分页结果
        return new PageResult<>(this.baseMapper.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<BizDeviceStatusAssess> list(BizDeviceStatusAssessParam bizDeviceStatusAssessParam) {

        // 构造条件
        LambdaQueryWrapper<BizDeviceStatusAssess> queryWrapper = new LambdaQueryWrapper<>();
        if(bizDeviceStatusAssessParam != null && ObjectUtil.isNotEmpty(bizDeviceStatusAssessParam.getDeviceId())){
            queryWrapper.eq(BizDeviceStatusAssess::getDeviceId,bizDeviceStatusAssessParam.getDeviceId());
        }
        return this.list(queryWrapper);
    }

    @Override
    public Integer add(BizDeviceStatusAssessParam bizDeviceStatusAssessParam) {
        // 将dto转为实体
        BizDeviceStatusAssess bizDeviceStatusAssess = new BizDeviceStatusAssess();
        BeanUtil.copyProperties(bizDeviceStatusAssessParam, bizDeviceStatusAssess);

        this.save(bizDeviceStatusAssess);
        return bizDeviceStatusAssess.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizDeviceStatusAssessParam bizDeviceStatusAssessParam) {

        //删除子设备状态评估信息
        final LambdaQueryWrapper<BizDeviceStatusAssessInfo> infoWrapper = new LambdaQueryWrapper<>();
        infoWrapper.eq(BizDeviceStatusAssessInfo::getAssessId,bizDeviceStatusAssessParam.getId());
        bizDeviceStatusAssessInfoService.remove(infoWrapper);

        this.removeById(bizDeviceStatusAssessParam.getId());
    }

    @Override
    public void edit(BizDeviceStatusAssessParam bizDeviceStatusAssessParam) {

        // 根据id查询实体
        BizDeviceStatusAssess bizDeviceStatusAssess = this.queryBizDeviceStatusAssess(bizDeviceStatusAssessParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bizDeviceStatusAssessParam, bizDeviceStatusAssess);

        this.updateById(bizDeviceStatusAssess);
    }

    @Override
    public BizDeviceStatusAssess detail(BizDeviceStatusAssessParam bizDeviceStatusAssessParam) {
        return this.queryBizDeviceStatusAssess(bizDeviceStatusAssessParam);
    }

    @Override
    public void export(BizDeviceStatusAssessParam bizDeviceStatusAssessParam) {
        Integer assessParamId = bizDeviceStatusAssessParam.getId();
        LambdaQueryWrapper<BizDeviceStatusAssessInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizDeviceStatusAssessInfo::getAssessId,assessParamId);
        queryWrapper.last("limit 1");
        BizDeviceStatusAssessInfo bizDeviceStatusAssessInfo = bizDeviceStatusAssessInfoService.getOne(queryWrapper);
        if(ObjectUtil.isNotNull(bizDeviceStatusAssessInfo)){
            String exceptionIds = bizDeviceStatusAssessInfo.getExceptionIds();
            String[] arr = exceptionIds.substring(1, exceptionIds.length() - 1).split(", "); // removes the brackets and splits the string into an array
            int[] intArr = new int[arr.length];
            for (int i = 0; i < arr.length; i++) {
                intArr[i] = Integer.parseInt(arr[i]); // converts each string element to an integer
            }
            List<DeviceStatusAssessInfoResult> assessInfoList = bizDeviceStatusAssessInfoService.getAssessInfoList(intArr);
            PoiUtil.exportExcelWithStream("设备状态评估报告.xls", DeviceStatusAssessInfoResult.class, assessInfoList);
        }
    }

    /**
     * 获取通道类型
     *
     * @author ssy
     * @since 2022/02/16 10:59
     */
    private BizDeviceStatusAssess queryBizDeviceStatusAssess(BizDeviceStatusAssessParam bizDeviceStatusAssessParam) {
        BizDeviceStatusAssess bizDeviceStatusAssess = this.getById(bizDeviceStatusAssessParam.getId());
        return bizDeviceStatusAssess;
    }
}
